﻿/**
*用地平衡、用地强度预处理
*@module modules.analysis
*@class DCI.AutoGP
*@constructor initialize
*@extends Class
*/
define("analysis/autogp", [
    "leaflet",
    "core/dcins",
    "plugins/scrollbar",
    "analysis/gpHandler"
], function (L) {
    L.DCI.AutoGP = L.Class.extend({
        /**
        *类ID
        *@property id
        *@type {String}
        */
        id: 'AutoGP',
        /**
        *预处理结果
        *@property presult
        *@type {Array}
        */
        presult: [],

        /**
        *行政区划图形
        *@property Area
        *@type {Array}
        */
        Area: [],

        num: 0,

        sendNum:0,

        GPtype: "lb",

        /**
        *初始化
        *@method initialize
        */
        initialize: function () {
            var doPre = Project_ParamConfig.LandPre.doPre;
            if (doPre) {
                L.dci.app.services.baseService.whetherPre({
                    context: this,
                    success: function (yn) {
                        if (yn) {
                            this.getDivisions();
                        }
                    },
                    error: function () {
                        //报错则不执行任何操作，静默
                    }
                });
            }
        },

        /**
       *获取行政区划信息
       *@method getDivisions
       */
        getDivisions: function () {
            _this = this;
            L.dci.app.services.baseService.getXingzheng({

                success: function (res) {
                    for (var rn in res) {
                        _this.presult.push({ name: res[rn].RegionName, region: '', landb_result: '', lands_result: '' });
                    }
                    //一、获取完毕行政区划信息后，再开始执行行政区划边界查询操作
                    _this.doPre();

                    //_this._presult = { "name": "澄江街道", "landb_result": '[{ "name": "居住用地", "MARK": "R", "area": 24049419.15, "popu": 0, "popuarea": 0, "scale": "31.21%", "items": [{ "name": "二类居住用地", "MARK": "R2", "area": 19213289.43, "popu": 0, "popuarea": 0, "scale": "24.94%", "items": [{ "name": "住宅用地", "MARK": "R21", "area": 111220.42, "popu": 0, "popuarea": 0, "scale": "0.14%", "item": [] }] }] }, { "name": "公共管理与公共服务用地", "MARK": "A", "area": 3249932.28, "popu": 0, "popuarea": 0, "scale": "4.22%", "items": [{ "name": "行政办公用地", "MARK": "A1", "area": 580166.27, "popu": 0, "popuarea": 0, "scale": "0.75%", "items": [] }, { "name": "文化设施用地", "MARK": "A2", "area": 581914.15, "popu": 0, "popuarea": 0, "scale": "0.76%", "items": [] }, { "name": "教育科研用地", "MARK": "A3", "area": 1024846.57, "popu": 0, "popuarea": 0, "scale": "1.33%", "items": [{ "name": "高等院校用地", "MARK": "A31", "area": 73828.73, "popu": 0, "popuarea": 0, "scale": "0.10%", "item": [] }, { "name": "中等专业学校用地", "MARK": "A32", "area": 885016.72, "popu": 0, "popuarea": 0, "scale": "1.15%", "item": [] }, { "name": "中小学用地", "MARK": "A33", "area": 21006.91, "popu": 0, "popuarea": 0, "scale": "0.03%", "item": [] }, { "name": "特殊教育用地", "MARK": "A34", "area": 44994.22, "popu": 0, "popuarea": 0, "scale": "0.06%", "item": [] }] }, { "name": "体育用地", "MARK": "A4", "area": 324860.21, "popu": 0, "popuarea": 0, "scale": "0.42%", "items": [] }, { "name": "医疗卫生用地", "MARK": "A5", "area": 187689.43, "popu": 0, "popuarea": 0, "scale": "0.24%", "items": [] }, { "name": "社会福利设施用地", "MARK": "A6", "area": 195342.76, "popu": 0, "popuarea": 0, "scale": "0.25%", "items": [] }, { "name": "文物古迹用地", "MARK": "A7", "area": 198624.05, "popu": 0, "popuarea": 0, "scale": "0.26%", "items": [] }, { "name": "宗教设施用地", "MARK": "A9", "area": 49058.87, "popu": 0, "popuarea": 0, "scale": "0.06%", "items": [] }] }, { "name": "商业服务业设施用地", "MARK": "B", "area": 6701095.64, "popu": 0, "popuarea": 0, "scale": "8.70%", "items": [{ "name": "商业设施用地", "MARK": "B1", "area": 4753150.46, "popu": 0, "popuarea": 0, "scale": "6.17%", "items": [] }, { "name": "商务设施用地", "MARK": "B2", "area": 1557861.33, "popu": 0, "popuarea": 0, "scale": "2.02%", "items": [] }, { "name": "娱乐康体设施用地", "MARK": "B3", "area": 133137.22, "popu": 0, "popuarea": 0, "scale": "0.17%", "items": [] }, { "name": "公用设施营业网点用地", "MARK": "B4", "area": 94469.65, "popu": 0, "popuarea": 0, "scale": "0.12%", "items": [{ "name": "加油加气站用地", "MARK": "B41", "area": 88174.16, "popu": 0, "popuarea": 0, "scale": "0.11%", "item": [] }, { "name": "其它公用设施营业网点用地", "MARK": "B49", "area": 6295.49, "popu": 0, "popuarea": 0, "scale": "0.01%", "item": [] }] }, { "name": "其它服务设施用地", "MARK": "B9", "area": 72923.02, "popu": 0, "popuarea": 0, "scale": "0.09%", "items": [] }] }, { "name": "工业用地", "MARK": "M", "area": 949346.63, "popu": 0, "popuarea": 0, "scale": "1.23%", "items": [{ "name": "二类工业用地", "MARK": "M2", "area": 886843.33, "popu": 0, "popuarea": 0, "scale": "1.15%", "items": [] }] }, { "name": "物流仓储用地", "MARK": "W", "area": 19554.31, "popu": 0, "popuarea": 0, "scale": "0.03%", "items": [{ "name": "一类物流仓储用地", "MARK": "W1", "area": 19554.31, "popu": 0, "popuarea": 0, "scale": "0.03%", "items": [] }] }, { "name": "道路与交通设施用地", "MARK": "S", "area": 1067621.11, "popu": 0, "popuarea": 0, "scale": "1.39%", "items": [{ "name": "交通枢纽用地", "MARK": "S3", "area": 493957.32, "popu": 0, "popuarea": 0, "scale": "0.64%", "items": [] }, { "name": "交通场站用地", "MARK": "S4", "area": 562465.7, "popu": 0, "popuarea": 0, "scale": "0.73%", "items": [{ "name": "公共交通场站用地", "MARK": "S41", "area": 272471.72, "popu": 0, "popuarea": 0, "scale": "0.35%", "item": [] }, { "name": "社会停车场用地", "MARK": "S42", "area": 204295.44, "popu": 0, "popuarea": 0, "scale": "0.27%", "item": [] }] }, { "name": "其它交通设施用地", "MARK": "S9", "area": 11198.1, "popu": 0, "popuarea": 0, "scale": "0.01%", "items": [] }] }, { "name": "公用设施用地", "MARK": "U", "area": 661942.27, "popu": 0, "popuarea": 0, "scale": "0.86%", "items": [{ "name": "供应设施用地", "MARK": "U1", "area": 318946.53, "popu": 0, "popuarea": 0, "scale": "0.41%", "items": [{ "name": "供水用地", "MARK": "U11", "area": 49215.3, "popu": 0, "popuarea": 0, "scale": "0.06%", "item": [] }, { "name": "供电用地", "MARK": "U12", "area": 100990.63, "popu": 0, "popuarea": 0, "scale": "0.13%", "item": [] }, { "name": "通信设施用地", "MARK": "U15", "area": 80714.18, "popu": 0, "popuarea": 0, "scale": "0.10%", "item": [] }, { "name": "广播电视设施用地", "MARK": "U16", "area": 34694.33, "popu": 0, "popuarea": 0, "scale": "0.05%", "item": [] }] }, { "name": "环境设施用地", "MARK": "U2", "area": 256560.66, "popu": 0, "popuarea": 0, "scale": "0.33%", "items": [{ "name": "排水设施用地", "MARK": "U21", "area": 203871.76, "popu": 0, "popuarea": 0, "scale": "0.26%", "item": [] }, { "name": "环卫设施用地", "MARK": "U22", "area": 52688.9, "popu": 0, "popuarea": 0, "scale": "0.07%", "item": [] }] }, { "name": "安全设施用地", "MARK": "U3", "area": 62803.84, "popu": 0, "popuarea": 0, "scale": "0.08%", "items": [{ "name": "消防设施用地", "MARK": "U31", "area": 60743.97, "popu": 0, "popuarea": 0, "scale": "0.08%", "item": [] }, { "name": "防洪设施用地", "MARK": "U32", "area": 2059.87, "popu": 0, "popuarea": 0, "scale": "0.00%", "item": [] }] }, { "name": "其它公用设施用地", "MARK": "U9", "area": 23631.25, "popu": 0, "popuarea": 0, "scale": "0.03%", "items": [] }] }, { "name": "绿地与广场用地", "MARK": "G", "area": 15897655.33, "popu": 0, "popuarea": 0, "scale": "20.63%", "items": [{ "name": "公园绿地", "MARK": "G1", "area": 8789598.07, "popu": 0, "popuarea": 0, "scale": "11.41%", "items": [] }, { "name": "防护绿地", "MARK": "G2", "area": 6999886.16, "popu": 0, "popuarea": 0, "scale": "9.08%", "items": [] }, { "name": "广场用地", "MARK": "G3", "area": 108171.1, "popu": 0, "popuarea": 0, "scale": "0.14%", "items": [] }] }, { "name": "建设用地", "MARK": "H", "area": 1369447.17, "popu": 0, "popuarea": 0, "scale": "1.78%", "items": [{ "name": "区域交通设施用地", "MARK": "H2", "area": 23526.99, "popu": 0, "popuarea": 0, "scale": "0.03%", "items": [{ "name": "公路用地", "MARK": "H22", "area": 23526.99, "popu": 0, "popuarea": 0, "scale": "0.03%", "item": [] }] }, { "name": "特殊用地", "MARK": "H4", "area": 1241757.71, "popu": 0, "popuarea": 0, "scale": "1.61%", "items": [{ "name": "军事用地", "MARK": "H41", "area": 1238466.66, "popu": 0, "popuarea": 0, "scale": "1.61%", "item": [] }, { "name": "安保用地", "MARK": "H42", "area": 3291.04, "popu": 0, "popuarea": 0, "scale": "0.00%", "item": [] }] }, { "name": "其它建设用地", "MARK": "H9", "area": 104162.47, "popu": 0, "popuarea": 0, "scale": "0.14%", "items": [] }] }, { "name": "非建设用地", "MARK": "E", "area": 17717690.74, "popu": 0, "popuarea": 0, "scale": "22.99%", "items": [{ "name": "水域", "MARK": "E1", "area": 14993939.5, "popu": 0, "popuarea": 0, "scale": "19.46%", "items": [{ "name": "自然水域", "MARK": "E11", "area": 11568406.51, "popu": 0, "popuarea": 0, "scale": "15.01%", "item": [] }] }, { "name": "农林用地", "MARK": "E2", "area": 2723751.24, "popu": 0, "popuarea": 0, "scale": "3.54%", "items": [] }] }]', "lands_result": '[[["1143.00", "51.00%", "40157975.60", "52.17%"], ["166.00", "7.00%", "5261440.50", "6.83%"], ["273.00", "12.00%", "13159623.40", "17.09%"], ["385.00", "17.00%", "13740393.70", "17.85%"], ["239.00", "10.00%", "4663164.10", "6.06%"]], [["446.00", "20.00%", "25425661.50", "33.03%"], ["371.00", "16.00%", "6934946.30", "9.01%"], ["281.00", "12.00%", "8178353.20", "10.62%"], ["436.00", "19.00%", "21849260.90", "28.38%"], ["672.00", "30.00%", "14594375.40", "18.96%"]], [["1101.00", "49.00%", "39410013.90", "51.19%"], ["4.00", "0.00%", "39376.60", "0.05%"], ["31.00", "1.00%", "1008589.40", "1.31%"], ["473.00", "21.00%", "23347874.60", "30.33%"], ["597.00", "27.00%", "13176742.80", "17.12%"]]]' };
                    //_this._presult = JSON.stringify(_this._presult);
                    //////var doc = new ActiveXObject("Microsoft.XMLDOM");

                    ////var XmlDoc = _this.createXMLDoc();  //解决浏览器兼容
                    ////var Root = XmlDoc.createElement("students");
                    ////Root.setAttribute("xmlns","http://www.w3.org/TR/html4/");
                    ////Root.setAttribute("version","1.0");
                    ////Element = XmlDoc.createElement("name");
                    ////text = XmlDoc.createTextNode("sss");
                    ////Element.appendChild(text);
                    ////Root.appendChild(Element);
                    ////XmlDoc.appendChild(Root);


                    ////L.dci.app.util.dialog.alert(serializeXMLDoc(XmlDoc));


                    //L.dci.app.services.baseService.saveLandPre({
                    //    async: true,
                    //    data: _this._presult,
                    //    context: this,
                    //    success: function (ss) {
                    //        L.dci.app.util.dialog.alert("温馨提示", "执行完毕");
                    //    },
                    //    error: function (XMLHttpRequest, errorThrown) {
                    //        L.dci.app.util.dialog.alert("温馨提示", "执行错误");
                    //    }
                    //});


                },
                error: function () {
                    L.DCI.app.util.dialog.alert("错误提示", "行政区划信息获取失败，请检查该服务。");
                }
            });
        },

        createXMLDoc:function() {
          try //Internet Explorer
            {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        }catch(e) {
            try //Firefox, Mozilla, Opera, etc.
        {
              var xmlDoc = document.implementation.createDocument("","",null);
        } catch(e) {
            alert(e.message)
        }
        }
        return xmlDoc;
        },
        serializeXMLDoc:function(doc) {
            var text;
            try {
                text = (new XMLSerializer()).serializeToString(doc);
            } catch(e) {
                text = doc.xml;
            }
            return text;
        },



        /**
       *执行用地平衡分析预处理
       *@method doPre
       */
        doPre: function () {
            var _this = this;
            var url = Project_ParamConfig.ydphfxConfig.xingzheng_layer.url;
            var field = Project_ParamConfig.ydphfxConfig.xingzheng_layer.field;
            var text = this.presult[this.num].name;
            var layerIndex = "1";

            var find = new L.esri.Tasks.find(url);
            find.layers(layerIndex).text(text).fields(field);
            find.run(function (error, featureCollection, response) {
                if (error) {
                    _this.num++;
                    if (_this.num == _this.presult.length - 1) {
                        _this.num = 0;
                        _this.getService();
                        //L.dci.app.util.dialog.alert("温馨提示", "区域查询完成");
                    } else
                        _this.doPre();
                    L.dci.app.util.dialog.alert("代码:" + error.code, "详情:" + error.message);
                } else {
                    if (featureCollection != null) {
                        var feature = featureCollection.features[0];

                        var coordinates = feature.geometry.coordinates;
                        for (var i = 0; i < coordinates[0].length; i++) {
                            var cache = coordinates[0][i][0];
                            coordinates[0][i][0] = coordinates[0][i][1];
                            coordinates[0][i][1] = cache;
                        }
                        var lay = L.polygon(coordinates);

                        var featureSet = new L.DCI.FeatureSet();
                        featureSet.features = [lay];
                        _this.presult[_this.num].region = featureSet;

                    } else {
                        L.dci.app.util.dialog.alert("温馨提示", "没有区域查询结果");
                    }
                    _this.num++;
                    if (_this.num == _this.presult.length) {
                        _this.num = 0;
                        //二、查询完毕所有行政区划的边界并储存，然后开始依次执行用地平衡分析操作；
                        _this._getService();
                        //L.dci.app.util.dialog.alert("温馨提示", "区域查询完成");
                    } else
                        _this.doPre();
                }
            });
        },

        /**
        *请求GP服务
        *@method getService
        *@param type{string} 执行GP分析类型
        *@private
        */
        _getService: function () {
            var type = this.GPtype;
            if (type == "lb") {
                Project_ParamConfig.ydphfxConfig.inputParams.In_region = this.presult[this.num].region;
                var url = Project_ParamConfig.ydphfxConfig.url;
                var gp = new L.DCI.GPHandler();
                gp.GPHandler(url, Project_ParamConfig.ydphfxConfig.inputParams, Project_ParamConfig.ydphfxConfig.outParams, $.proxy(this.returnGPData, this), $.proxy(this.resultImageLayerHandler, this), $.proxy(this.errorHandler, this));
            } else if (type == "ls") {
                Project_ParamConfig.ydkfqdpjConfig.inputParams.In_region = this.presult[this.num].region;
                var url = Project_ParamConfig.ydkfqdpjConfig.url;
                var gp = new L.DCI.GPHandler();
                gp.GPHandler(url, Project_ParamConfig.ydkfqdpjConfig.inputParams, Project_ParamConfig.ydkfqdpjConfig.outParams, $.proxy(this.returnGPData, this), $.proxy(this.resultImageLayerHandler, this), $.proxy(this.errorHandler, this));
            }



        },


        /**
        *处理返回的GP结果
        *@method returnGPData
        *@param res{Object} Json格式结果
        *@private
        */
        returnGPData: function (res) {
            //隐藏正在加载
            var type = this.GPtype;
            if (type == "lb") {
                //如果结果有空值，则本次预处理结果全部不保存
                if (res.value == null) {
                    this.num = this.presult.length + 1;
                } else {
                    this.presult[this.num].landb_result = res.value;
                    this.num++;
                    if (this.num == this.presult.length) {
                        this.num = 0;
                        //三、执行用地平衡分析操作后，再执行用地强度评价分析；
                        this.GPtype = "ls";
                        this._getService();
                        //L.dci.app.util.dialog.alert("温馨提示", "执行完毕");
                    } else
                        this._getService();
                }
            } else if (type == "ls") {
                if (res.value == null) {
                    this.num = this.presult.length + 1;
                } else {
                    this.presult[this.num].lands_result = res.value;

                    this.presult[this.num].region = '';
                    this.num++;
                    if (this.num == this.presult.length) {
                        //后台保存代码
                        this._sendPre();
                    } else
                        this._getService();
                }
            }


        },




        _sendPre: function () {           
            if (this.sendNum == this.presult.length) {
                var statu = "优化完毕";
                //L.dci.app.util.dialog.alert("温馨提示", "系统自动优化完毕,祝您工作愉快");
            } else {
                var preJson = '';
                preJson += '{"num" : "' + this.sendNum + '","length" : "' + this.presult.length + '","name" : "' + this.presult[this.sendNum].name + '","landb_result":\'' + JSON.stringify(this.presult[this.sendNum].landb_result) + '\',"lands_result":\'' + JSON.stringify(this.presult[this.sendNum].lands_result) + '\'}';


                _this = this;
                L.dci.app.services.baseService.saveLandPre({
                    async: true,
                    data: preJson,
                    context: this,
                    success: function (ss) {
                        _this.sendNum++;
                        _this._sendPre();
                    },
                    error: function (XMLHttpRequest, errorThrown) {
                        L.dci.app.util.dialog.alert("温馨提示", "执行错误");
                    }
                });

            }
        },




        /**
        *返回的错误信息
        *@method errorHandler
        *@private
        */
        errorHandler: function () {
            this.num = 0;
        },


    });
    return L.DCI.AutoGP;
});
